﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 第六次作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string url = txtUrl.Text.Trim();
            if (string.IsNullOrEmpty(url))
            {
                MessageBox.Show("请输入有效的URL");
                return;
            }

            string pageContent = GetPageContent(url);
            if (string.IsNullOrEmpty(pageContent))
            {
                MessageBox.Show("无法获取页面内容，请检查网络连接或URL是否正确。");
                return;
            }

            // 正则表达式匹配手机号码和邮箱
            string phonePattern = @"(1[3-9]\d{9})";
            string emailPattern = @"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+";

            // 匹配手机号码和邮箱
            MatchCollection phoneMatches = Regex.Matches(pageContent, phonePattern);
            MatchCollection emailMatches = Regex.Matches(pageContent, emailPattern);

            // 显示结果
            rtbResult.Clear();
            rtbResult.Text += "手机号码：\n";
            foreach (Match match in phoneMatches)
            {
                rtbResult.Text += match.Value + "\n";
            }

            rtbResult.Text += "\n邮箱：\n";
            foreach (Match match in emailMatches)
            {
                rtbResult.Text += match.Value + "\n";
            }
        }

        private string GetPageContent(string url)
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    return client.DownloadString(url);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"获取页面内容出错: {ex.Message}");
                return null;
            }
        }
    }
}